import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
let userInfo = JSON.parse(localStorage.getItem('userInfo')) || {};
const store = new Vuex.Store({
  state: {
    userInfo: userInfo
  },
  mutations: {
    // 保存用户信息
    saveInfo(state, payload) {
      // 存到vuex中
      state.userInfo = payload;
      // 存到localStorage中
      localStorage.setItem('userInfo', JSON.stringify(payload));
    },
    // 清除用户信息
    clearInfo(state) {
      state.userInfo = {};
      localStorage.removeItem('userInfo');
    }
  }
})

export default store